package com.dynamic.mybatis.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

/**
 * @Description
 * @Author xs
 * @Date 2021/3/26 16:55
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }


    public static <T> Collection<T> get(Class<T> classType){
        Map<String, T> list =  applicationContext.getBeansOfType(classType);
        if(list == null || list.size() < 1){
            return null;
        }
       return list.values();
    }


}